<template>
  <div>
    <h1>登录</h1>
    <p>
      <input type="text" placeholder="请输入用户名" v-model="list.name" />
    </p>
    <p>
      <input type="text" placeholder="请输入密码" v-model="list.pwd" />
    </p>
    <p>
      <button @click="deng()">登录</button>
    </p>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { login } from "../../api/api";
import { useRouter } from "vue-router";

const router = useRouter();

const list = reactive({
  name: "",
  pwd: "",
});

const deng = () => {
  login(list.name, list.pwd).then((res) => {
    console.log(res);
    if (res.data.code == 0) {
      alert("用户名不存在");
    }
    if (res.data.code == 1) {
      alert("密码不正确");
    }
    if (res.data.code == 2) {
      let token = res.data.token;
      localStorage.setItem("token", token);
      router.push("/home");
    }
  });
};
</script>

<style scoped></style>